# -*- coding: utf-8 -*-
# @File     : serializers.py
# @Author   : bingjia
# @Time     : 2020/8/8 14:56
# @Desc     : 项目序列化

from django.apps import apps

from ..base.api import serializers

from . import models


class RoleSerializer(serializers.ModelSerializer):
    """
    成员序列化
    """
    permissions = serializers.SerializerMethodField()

    class Meta:
        model = models.Role
        fields = ['id', 'name', 'slug', 'permissions']

    def get_permissions(self, obj):
        return obj.permissions


class MembershipSerializer(serializers.ModelSerializer):
    """
    成员序列化
    """
    full_name = serializers.SerializerMethodField()
    role_name = serializers.SerializerMethodField()
    project_name = serializers.SerializerMethodField()

    class Meta:
        model = models.Membership
        fields = ['id', 'full_name', 'role_name', 'project_name', 'is_admin', 'created_date']

    def get_full_name(self, obj):
        return obj.user.full_name

    def get_role_name(self, obj):
        return obj.role.name

    def get_project_name(self, obj):
        return obj.project.name


class ProjectSerializer(serializers.ModelSerializer):
    """
    项目序列化
    """
    owner = serializers.SerializerMethodField()

    class Meta:
        model = models.Project
        fields = ['id', 'name', 'company', 'owner', 'operation_date', 'project_type',
                  'image_status', 'analysis_status', 'view_status', 'analysis_progress']

    def get_owner(self, obj):
        return obj.owner.full_name


class ProjectAdminSerializer(serializers.ModelSerializer):
    """
    项目管理序列化
    """
    owner = serializers.SerializerMethodField()

    class Meta:
        model = models.Project
        fields = ['id', 'name', 'company', 'owner', 'operation_date',
                  'image_status', 'analysis_status', 'view_status', 'analysis_progress']

    def get_owner(self, obj):
        return obj.owner.full_name
